﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Web.czk;

namespace Web
{
    public partial class zb : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["sn"]=="1984")
            {
                zhuce.Visible = true;
                MMIP.Visible = true;
            }
        }

        private string Encrypt(string encryptString, string encryptKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Mode = System.Security.Cryptography.CipherMode.ECB;
            byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(encryptString);
            //建立加密对象的密钥和偏移量   
            //原文使用ASCIIEncoding.ASCII方法的GetBytes方法   
            //使得输入密码必须输入英文文本   
            des.Key = ASCIIEncoding.ASCII.GetBytes(encryptKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(encryptKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }

        protected void zhuce_Click(object sender, EventArgs e)
        {
            try
            {
                string diskid = SNTB.Text.Trim();
                StringBuilder sb = new StringBuilder();
                if (lmcb.Checked)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
                if (zjcb.Checked)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
                if (phbcb.Checked)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
                if (cjcb.Checked)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
                if (xqcb.Checked)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
                if (pkcb.Checked)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
                if (xqpkcb.Checked)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
                if (spxqcb.Checked)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
                sb.Append("0000000000");

                string xq = Encrypt(sb.ToString() + diskid, "ljsheng1");
                string cz = Encrypt(czk.Text.Trim() + diskid, "ljsheng1");
                if (string.IsNullOrEmpty(czk.Text.Trim()))
                {
                    cz = "";
                }
                else
                {
                    AddCZK(czk.Text.Trim().Substring(10, 2) + diskid, cz);
                    if (!string.IsNullOrEmpty(MMIP.Text))
                    {
                        string path = System.Web.HttpContext.Current.Server.MapPath("/czk/mm/");
                        string filename = path + diskid + ".html";
                        StreamWriter sr = null;
                        try
                        {
                            if (File.Exists(filename))
                            {
                                File.Delete(filename);
                            }
                            sr = File.CreateText(filename);
                            sr.WriteLine(MMIP.Text);
                        }
                        catch
                        {
                        }
                        finally
                        {
                            if (sr != null)
                            {
                                sr.Close();
                                sr.Dispose();
                            }
                        }
                    }
                }
                if (sb.ToString().Contains("1"))
                {
                    AddCZK(diskid, xq);
                }
                else
                {
                    xq = "";
                }
                Response.Write(xq + "\r\n" + cz);
            }
            catch { }
        }

        protected void AddCZK(string diskid, string czk)
        {
            //try
            //{
            //    string path = System.Web.HttpContext.Current.Server.MapPath("/czk/");
            //    INIFile db = new INIFile(path + "czk.ini");
            //    db.IniWriteValue("czk", diskid, czk);
            //}
            //catch (Exception err)
            //{
            //    Response.Write(err.Message);
            //}
            string path = System.Web.HttpContext.Current.Server.MapPath("/czk/");
            string filename = path + diskid + ".html";
            if (WLCB.Checked)
            {
                StreamWriter sr = null;
                try
                {
                    if (File.Exists(filename))
                    {
                        File.Delete(filename);
                    }
                    sr = File.CreateText(filename);
                    sr.WriteLine(czk);
                }
                catch
                {
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();
                        sr.Dispose();
                    }
                }
            }

            if (SCB.Checked && File.Exists(filename))
            {
                File.Delete(filename);
            }
        }
    }
}